package org.jeecg.modules.middleware.quartz.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.paycenter.dto.BankCfgDto;
import org.jeecg.modules.paycenter.service.IPayChannelService;
import org.jeecg.modules.paycenter.service.PayCenterService;
import org.jeecg.modules.paycenter.service.PayChannelFactory;
import org.jeecg.modules.wlhy.bill.entity.HyShippingNoteBill;
import org.jeecg.modules.wlhy.bill.service.IHyCustomerBillService;
import org.jeecg.modules.wlhy.bill.service.IHyShippingNoteBillService;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Description: 下载电子回单定时任务
 * @author: luruirui
 * @date: 2021年07月15日 17:53
 */
//@Service
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
public class BankDownloadJobService implements Job {
    @Autowired
    IHyCustomerBillService hyCustomerBillService;

    @Autowired
    IHyShippingNoteBillService hyShippingNoteBillService;

    @Autowired
    private PayCenterService payCenterService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<BankCfgDto> ptAccountCfgList = payCenterService.getPtAccountCfgList();
        Long start = System.currentTimeMillis();
        log.info("定时下载运费流水单电子回单开始:" + DateUtils.getDateStr());
        for (BankCfgDto bankCfgDto : ptAccountCfgList) {
            String channel = bankCfgDto.getChannel();
            QueryWrapper<HyShippingNoteBill> queryWrapper =  new QueryWrapper<>();
            queryWrapper.eq("resp_source", "0");
            queryWrapper.eq("resp_code", "4");
            queryWrapper.eq("pay_channel",channel);
            queryWrapper.isNull("bank_hd");
            queryWrapper.orderByDesc("create_time");
            try {
                List<HyShippingNoteBill> shippingNoteBills = hyShippingNoteBillService.list(queryWrapper);
                if (shippingNoteBills.size() > 0) {
                    IPayChannelService payChannel = PayChannelFactory.getPayChannel(channel);
                    payChannel.batchDownloadShippingBillHd(shippingNoteBills);
                }
            }catch (Exception ex){

            }
        }
        log.info("定时下载运费流水单电子回单结束,耗时:{}毫秒" ,System.currentTimeMillis() - start);
    }

}
